import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { AppManager } from '../../native/app-manager';
import { Launcher } from '../../native/launcher';
import { Loading } from '../../components/loading';

@Component({
  selector: 'page-list',
  templateUrl: 'list.html'
})
export class ListPage {

  apps: Array<any>;

  constructor(
    public navCtrl: NavController, 
    public navParams: NavParams,
    private appManger: AppManager,
    private launcher: Launcher,
    private loading: Loading,
    ) {
    this.loading.presentLoading();
    this.appManger.query(true)
    .then((res: any) => {
      this.apps = res.map(app =>{
        app.icon = 'data:image/jpeg;base64,' + app.icon;
        return app;
      });
      this.loading.dismiss();
    })
    .catch((error: any) => console.error(error));
  }

  luanchApp(packageName: string):void {

    this.loading.presentLoading();
    this.launcher.canLaunch({packageName:packageName})
    .then(res => {
      console.log(res);
      this.launcher.launch({packageName:packageName})
      .then(s=>{
        console.log(s);
        this.loading.dismiss();
      })
      .catch(e=>console.log(e))
    }).catch(er=>console.log(er));
  }
}
